<?php

class Sistema_EmpresaController extends App_Controller_Action
{

    private $_table     = 'EMPRESA';
    private $_pk        = 'EMP_ID';
    private $_order_col = 'EMP_NOM';
    private $_order     = 'ASC';
    private $_sta_col   = 'EMP_STA';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'EMP_FILE'; 
    public $dir = 'public/upload/empresa/';
    public $img_width  = 305;
    public $img_height = 140;
    //public  $db = '';
     
    public function init() 
    {   
        //$this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $this->obj = new Sistema_Model_DbTable_Empresa();
        //if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Empresa ();
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('elementosform','library');
        
        $where = $this->_pk . ' > 0';
       
        //$STA = $this->_getParam($this->_sta_col, 1);
        //$where .= ' and ' . $this->_sta_col . ' = ' . $STA;
        
        $dscfiltro = $this->_getParam('dscfiltro', '');
        $dscsta = $this->_getParam('dscsta', '1');
      
        if($dscfiltro > '') $where .= ' and EMP_NOM like "%'. $dscfiltro.'%"';
        if($dscsta > '') $where .= ' and EMP_STA = '. $dscsta;
        
        $this->view->dscfiltro = $dscfiltro;
        $this->view->dscsta = $dscsta;
        
        //$result = $this->obj->fetchAll($this->obj->select()->where($where)->order($this->_order_col.' '.$this->_order));
        $query = 'SELECT SQL_CACHE * FROM EMPRESA WHERE '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
    }
    
    public function editAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('imagem','library');
        Zend_Loader::loadClass('elemform','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_geral.js');
        
        $_SESSION['dir_url'] = $_SERVER['DOCUMENT_ROOT'].$this->_urlbase.'/';
        $_SESSION['dir_uploud'] = $_SESSION['dir_url'].$this->dir;
        $_SESSION['img_width']  = $this->img_width;
        $_SESSION['img_height'] = $this->img_height;
        $_SESSION['controller'] = '';//$this->_controller;
        $_SESSION['img_key'] = '';
        $_SESSION['img_id']  = '';
        $_SESSION['img_tumb'] = '0';
        
        //$this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if(1){ //($this->form->isValid($formData)) {
                    $id = (int) $formData[$this->_pk]; // $this->form->getValue($this->_pk);
                    
                    $returnImg = imagem::upload($this->_file_col,$this->dir,$id);
                    
                    $data = $formData;//$this->form->getValues();
                    
                    unset($data[$this->_file_col]);
                    unset($data['img']);
                    unset($data['files']);
                    
                    if($id == 0){
                        $this->obj->insert($data);
                        $id =  $this->obj->getAdapter()->lastInsertId();                    
                        imagem::rename($id,$this->dir,$returnImg['filenew']);
                    }
                    else $this->obj->update($data, $this->_pk.' = ' . $id);
                    
                    if(strlen($_FILES[$this->_file_col]['tmp_name']) > 1){
                        $img = glob($this->dir.$id.'.*');
                        foreach($img as $filetemp) $filename = $filetemp;
                        imagem::reizeFull($filename,$filename, '900', '300');
                    }  
                    
                    $this->_helper->redirector('index');
                } else {
                    //$this->form->populate($formData);
                    $this->view->data = $formData;
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                //echo $ex->getMessage();
                //$this->form->populate($formData);
                $this->view->data = $formData;
            }
        } else {
            
            $id = $this->_getParam('id', 0);
            
            if ($id > 0) {
                $data = $this->obj->fetchRow($this->_pk . ' = ' . $id);
                //$this->form->populate($data);
                $this->view->data = $data;
                $_SESSION['img_id'] = $data['EMP_ID'];
                $_SESSION['img_key'] = $data['EMP_ID'];
            }else{
                $data = array('EMP_NOM'=>'','EMP_STA' => '0');
                $this->obj->insert($data);
                $id =  $this->obj->getAdapter()->lastInsertId();                    
                $data = $this->obj->fetchRow($this->_pk . ' = ' . $id);
                $data['EMP_STA'] = 1;
                //$this->form->populate($data);
                $this->view->data = $data;
                $_SESSION['img_id'] = $data['EMP_ID'];
                $_SESSION['img_key'] = $data['EMP_ID'];
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {   
            $data = array($this->_sta_col => 0);
            $ok = (bool) $this->obj->update($data, $this->_pk . ' = ' . $id);
        }       
        $this->_helper->redirector('index');
        //echo json_encode(array('ok' => $ok));
    }
    
    public function delfotoAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {   
            
            Zend_Loader::loadClass('imagem','library');
            imagem::delete($id,$this->dir,TRUE);
            
            $filename = glob($this->dir.$id.'*.*');
            
            $ok = !file_exists($filename[0]);
        }       
        echo json_encode(array('ok' => (bool)$ok));
    }
}